//
//  caigoulistViewController.m
//  BAOASystem
//
//  Created by michael on 2018/3/6.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "PTLMaskingView.h"
#import "OALoginViewController.h"
#import "fajianxqViewController.h"
#import "caigoulistViewController.h"
#import "xinxilistTableViewCell.h"
#import "caigouxqViewController.h"
@interface caigoulistViewController ()<PTLMenuButtonDelegate>

@end

@implementation caigoulistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
        self.title = NSLocalizedString(@"采购信息", nil);
 
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, KScreenWidth,KScreenHeight-kTopHeight-30) style:UITableViewStylePlain];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;


_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delegate = self;
[self.view addSubview:_tableView];
 [MBProgressHUD showHUDAddedTo:self.view animated:YES];

 __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
//        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
   if (weakSelf.lastPage==0 ){

//        if (weakSelf.isSearching) {
////     [self loadSearchMore];
//}
//     else {
     [weakSelf loadMore];
//     }
        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];
            
        }
        
    }];
}
-(void)viewDidLayoutSubviews

{

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [self.tableView setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        [self.tableView setLayoutMargins:UIEdgeInsetsZero];

    }

}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

 

}
#pragma mark - PTLMenuButtonDelegate

-(void)loadData{
 [MBProgressHUD showHUDAddedTo:self.view animated:YES];

 NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
          dics[@"pageNumber"] = @"1";
  dics[@"pageSize"] = @"20";
   [dics setValue:_typeid forKey:@"typeid"];
   [dics setValue:_ctgid forKey:@"ctgid"];
   NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/demand/list?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
      _dic = dictionary;
[self initview];
  if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
   NSLog(@"%@",dictionary);
   NSDictionary *dic = dictionary[@"demandpage"];
NSArray *array = dic[@"list"];
if (array.count==0) {
                [self.view makeToast:NSLocalizedString(@"没有数据", nil)];

}
  _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
   _dataArray = [NSMutableArray array];
   for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
[_tableView reloadData];
            [self.tableView headerEndRefreshing];
        self.currentPage++;
 }andFailure:^{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.tableView headerEndRefreshing];


 }];
}
- (void)initview{
if (_btn) {
[_btn removeFromSuperview];
}
    _btn = [[PTLMenuButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30) menuTitles:@[NSLocalizedString(@"采购信息list-类型", nil),NSLocalizedString(@"采购信息list所属行业", nil)]];
    NSArray *array1 = _dic[@"typelist"];
    NSArray *array2 = _dic[@"ctglist"];
    NSMutableArray *listarray1 = [NSMutableArray array];
    NSDictionary *seltype =_dic[@"selType"];
    if ([seltype count ]!=0) {
        [listarray1 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),seltype[@"dictname"]]];

}
//    [listarray1 addObject:NSLocalizedString(@"清空搜索条件", nil)];

    for (NSDictionary *dic in array1) {
    [listarray1 addObject:dic[@"dictname"]];
}
  NSMutableArray *listarray2 = [NSMutableArray array];
  
   NSDictionary *selCtg =_dic[@"selCtg"];
    if ([selCtg count ]!=0) {
        [listarray2 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),selCtg[@"ctgName"]]];

}
//  [listarray2 addObject:NSLocalizedString(@"清空搜索条件", nil)];

    for (NSDictionary *dic in array2) {
    [listarray2 addObject:dic[@"ctgName"]];
}

    NSArray * listArr1 = listarray1;
    NSArray * listArr2 = listarray2;
    _btn.listTitles = @[listArr1, listArr2];
    _btn.delegate = self;
    [self.view addSubview:_btn];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 29, self.view.frame.size.width, 1)];
    view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
        [self.view addSubview:view];
     __weak typeof(self) weakSelf = self;

  [self.btn.listView.maskingView setButtonblock :^() {
  NSLog(@"11111");
    _typeid = @"";
    _ctgid = @"";
        [weakSelf loadData];


  }];



}
-(void)ptl_menuButton:(PTLMenuButton *)menuButton didSelectMenuButtonAtIndex:(NSInteger)index selectMenuButtonTitle:(NSString *)title listRow:(NSInteger)row rowTitle:(NSString *)rowTitle{
    NSLog(@"index: %zd, title:%@, listrow: %zd, rowTitle: %@", index, title, row, rowTitle);
      NSArray *array1 = _dic[@"typelist"];
    NSArray *array2 = _dic[@"ctglist"];
    
    if ([rowTitle isEqualToString:NSLocalizedString(@"清空搜索条件", nil)]){
    _typeid = @"";
    _ctgid = @"";
        [self loadData];

    }else if ([rowTitle containsString:NSLocalizedString(@"当前选择", nil)]){
    return;
    }
    else{
    if([title isEqualToString:NSLocalizedString(@"采购信息list-类型", nil)]){
    
    
     for (NSDictionary *dic in array1) {
   if ([dic[@"dictname"]isEqualToString:rowTitle]) {
    _typeid = dic[@"value"];
}
   
}
    [self loadData];
    }else{
     for (NSDictionary *dic in array2) {
   if ([dic[@"ctgName"]isEqualToString:rowTitle]) {
    _ctgid = dic[@"id"];
}
   
}
    [self loadData];
    }
    }
}

- (void)loadMore{
 NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
  dics[@"pageNumber"] = [NSString stringWithFormat:@"%d",_currentPage];
  dics[@"pageSize"] = @"20";
     [dics setValue:_typeid forKey:@"typeid"];
   [dics setValue:_ctgid forKey:@"ctgid"];
   NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/demand/list?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){

 
   NSLog(@"%@",dictionary);
      NSDictionary *dic = dictionary[@"demandpage"];

NSArray *array = dic[@"list"];
  _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
   for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;

 }andFailure:^{
  [self.tableView footerEndRefreshing];
      [MBProgressHUD hideHUDForView:self.view animated:YES];
//         [self.tableView headerEndRefreshing];

 }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  7*20+30;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"getProductLis";

  xinxilistTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (cell==nil) {
        cell=[[xinxilistTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 NSDictionary *dic = _dataArray[indexPath.row];
 caigouxqViewController *vc = [[caigouxqViewController alloc]init];
 vc.pdid =dic[@"id"];
 vc.hidesBottomBarWhenPushed = YES;
 [self.navigationController pushViewController:vc animated:YES];
 
 }
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
